﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using com.loto.Common.DBUtility;
using com.loto.Common.Model;

namespace com.huigo.Framework.SqlServerDal
{
    public partial class OrderInfo
    {
        public static long AddOrder(Model.OrderInfo orderInfo, SqlTransaction trans)
        {
            string sql = "insert into [HG_ORDER_INFO]({0}) values({1});SELECT @@IDENTITY;";
            List<string> f = new List<string>();
            List<string> v = new List<string>();
            List<SqlParameter> paraList = new List<SqlParameter>();
            SqlParameter para;

            if (!orderInfo.OrderId.Equals(long.MinValue))
            {
                f.Add(" ORDER_ID");
                v.Add(" @ORDER_ID");
                para = new SqlParameter("@ORDER_ID", SqlDbType.BigInt, 8);
                para.Value = orderInfo.OrderId;
                paraList.Add(para);
            }
            if (!orderInfo.OrderCustFid.Equals(long.MinValue))
            {
                f.Add(" ORDER_CUST_FID");
                v.Add(" @ORDER_CUST_FID");
                para = new SqlParameter("@ORDER_CUST_FID", SqlDbType.BigInt, 8);
                para.Value = orderInfo.OrderCustFid;
                paraList.Add(para);
            }
            if (!orderInfo.OrderProductFid.Equals(long.MinValue))
            {
                f.Add(" ORDER_PRODUCT_FID");
                v.Add(" @ORDER_PRODUCT_FID");
                para = new SqlParameter("@ORDER_PRODUCT_FID", SqlDbType.BigInt, 8);
                para.Value = orderInfo.OrderProductFid;
                paraList.Add(para);
            }
            if (!orderInfo.OrderInsCompanyName.Equals(string.Empty))
            {
                f.Add(" ORDER_INS_COMPANY_NAME");
                v.Add(" @ORDER_INS_COMPANY_NAME");
                para = new SqlParameter("@ORDER_INS_COMPANY_NAME", SqlDbType.NVarChar, 30);
                para.Value = orderInfo.OrderInsCompanyName;
                paraList.Add(para);
            }
            if (!orderInfo.OrderProductName.Equals(string.Empty))
            {
                f.Add(" ORDER_PRODUCT_NAME");
                v.Add(" @ORDER_PRODUCT_NAME");
                para = new SqlParameter("@ORDER_PRODUCT_NAME", SqlDbType.NVarChar, 50);
                para.Value = orderInfo.OrderProductName;
                paraList.Add(para);
            }
            if (!orderInfo.OrderSupName.Equals(string.Empty))
            {
                f.Add(" ORDER_SUP_NAME");
                v.Add(" @ORDER_SUP_NAME");
                para = new SqlParameter("@ORDER_SUP_NAME", SqlDbType.NVarChar, 20);
                para.Value = orderInfo.OrderSupName;
                paraList.Add(para);
            }
            if (!orderInfo.OrderInsTypeFid.Equals(long.MinValue))
            {
                f.Add(" ORDER_INS_TYPE_FID");
                v.Add(" @ORDER_INS_TYPE_FID");
                para = new SqlParameter("@ORDER_INS_TYPE_FID", SqlDbType.BigInt, 8);
                para.Value = orderInfo.OrderInsTypeFid;
                paraList.Add(para);
            }
            if (!orderInfo.OrderNo.Equals(string.Empty))
            {
                f.Add(" ORDER_NO");
                v.Add(" @ORDER_NO");
                para = new SqlParameter("@ORDER_NO", SqlDbType.NVarChar, 20);
                para.Value = orderInfo.OrderNo;
                paraList.Add(para);
            }
            if (!orderInfo.OrderAmount.Equals(decimal.MinValue))
            {
                f.Add(" ORDER_AMOUNT");
                v.Add(" @ORDER_AMOUNT");
                para = new SqlParameter("@ORDER_AMOUNT", SqlDbType.Money, 8);
                para.Value = orderInfo.OrderAmount;
                paraList.Add(para);
            }
            if (!orderInfo.OrderPayAmount.Equals(decimal.MinValue))
            {
                f.Add(" ORDER_PAY_AMOUNT");
                v.Add(" @ORDER_PAY_AMOUNT");
                para = new SqlParameter("@ORDER_PAY_AMOUNT", SqlDbType.Money, 8);
                para.Value = orderInfo.OrderPayAmount;
                paraList.Add(para);
            }
            if (!orderInfo.OrderDiscountAmount.Equals(decimal.MinValue))
            {
                f.Add(" ORDER_DISCOUNT_AMOUNT");
                v.Add(" @ORDER_DISCOUNT_AMOUNT");
                para = new SqlParameter("@ORDER_DISCOUNT_AMOUNT", SqlDbType.Money, 8);
                para.Value = orderInfo.OrderDiscountAmount;
                paraList.Add(para);
            }
            if (!orderInfo.OrderScoreAmount.Equals(decimal.MinValue))
            {
                f.Add(" ORDER_SCORE_AMOUNT");
                v.Add(" @ORDER_SCORE_AMOUNT");
                para = new SqlParameter("@ORDER_SCORE_AMOUNT", SqlDbType.Money, 8);
                para.Value = orderInfo.OrderScoreAmount;
                paraList.Add(para);
            }
            if (!orderInfo.OrderCreatedOn.Equals(DateTime.MinValue))
            {
                f.Add(" ORDER_CREATED_ON");
                v.Add(" @ORDER_CREATED_ON");
                para = new SqlParameter("@ORDER_CREATED_ON", SqlDbType.DateTime, 8);
                para.Value = orderInfo.OrderCreatedOn;
                paraList.Add(para);
            }
            if (!orderInfo.OrderPaidOn.Equals(DateTime.MinValue))
            {
                f.Add(" ORDER_PAID_ON");
                v.Add(" @ORDER_PAID_ON");
                para = new SqlParameter("@ORDER_PAID_ON", SqlDbType.DateTime, 8);
                para.Value = orderInfo.OrderPaidOn;
                paraList.Add(para);
            }
            if (!orderInfo.OrderLastUpdatedOn.Equals(DateTime.MinValue))
            {
                f.Add(" ORDER_LAST_UPDATED_ON");
                v.Add(" @ORDER_LAST_UPDATED_ON");
                para = new SqlParameter("@ORDER_LAST_UPDATED_ON", SqlDbType.DateTime, 8);
                para.Value = orderInfo.OrderLastUpdatedOn;
                paraList.Add(para);
            }
            if (!orderInfo.OrderSource.Equals(byte.MaxValue))
            {
                f.Add(" ORDER_SOURCE");
                v.Add(" @ORDER_SOURCE");
                para = new SqlParameter("@ORDER_SOURCE", SqlDbType.TinyInt, 1);
                para.Value = orderInfo.OrderSource;
                paraList.Add(para);
            }
            if (!orderInfo.OrderFrom.Equals(byte.MaxValue))
            {
                f.Add(" ORDER_FROM");
                v.Add(" @ORDER_FROM");
                para = new SqlParameter("@ORDER_FROM", SqlDbType.TinyInt, 1);
                para.Value = orderInfo.OrderFrom;
                paraList.Add(para);
            }
            if (!orderInfo.OrderStatusFid.Equals(int.MinValue))
            {
                f.Add(" ORDER_STATUS_FID");
                v.Add(" @ORDER_STATUS_FID");
                para = new SqlParameter("@ORDER_STATUS_FID", SqlDbType.Int, 4);
                para.Value = orderInfo.OrderStatusFid;
                paraList.Add(para);
            }
            if (!orderInfo.OrderProductCode.Equals(string.Empty))
            {
                f.Add(" ORDER_PRODUCT_CODE");
                v.Add(" @ORDER_PRODUCT_CODE");
                para = new SqlParameter("@ORDER_PRODUCT_CODE", SqlDbType.NVarChar, 20);
                para.Value = orderInfo.OrderProductCode;
                paraList.Add(para);
            }
            if (!orderInfo.OrderPersonCount.Equals(int.MinValue))
            {
                f.Add(" ORDER_PERSON_COUNT");
                v.Add(" @ORDER_PERSON_COUNT");
                para = new SqlParameter("@ORDER_PERSON_COUNT", SqlDbType.Int, 4);
                para.Value = orderInfo.OrderPersonCount;
                paraList.Add(para);
            }

            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = string.Format(sql, string.Join(",", f.ToArray()), string.Join(",", v.ToArray()));
            cmd.Parameters.AddRange(paraList.ToArray());
            try
            {
                return Convert.ToInt64(SqlHelper.ExecuteScalar(SqlHelper.MainSQLConnString, cmd, trans));
            }
            catch (Exception exce)
            {
                throw exce;
            }
        }
    }
}
